<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="app_id" prop="appId">
        <el-input v-model="formData.appId" placeholder="请输入app_id" />
      </el-form-item>
      <el-form-item label="app_key" prop="appKey">
        <el-input v-model="formData.appKey" placeholder="请输入app_key" />
      </el-form-item>
      <el-form-item label="第三方厂商平台id（cfs_third_platform的platform_id）" prop="thirdId">
        <el-input v-model="formData.thirdId" placeholder="请输入第三方厂商平台id（cfs_third_platform的platform_id）" />
      </el-form-item>
      <el-form-item label="对接区域编码" prop="areaCode">
        <el-input v-model="formData.areaCode" placeholder="请输入对接区域编码" />
      </el-form-item>
      <el-form-item label="对接区域名称" prop="areaName">
        <el-input v-model="formData.areaName" placeholder="请输入对接区域名称" />
      </el-form-item>
      <el-form-item label="社会运营商id（cfs_society_operater的operater_id）" prop="operatorId">
        <el-input v-model="formData.operatorId" placeholder="请输入社会运营商id（cfs_society_operater的operater_id）" />
      </el-form-item>
      <el-form-item label="社会运营中心名称" prop="thirdDataName">
        <el-input v-model="formData.thirdDataName" placeholder="请输入社会运营中心名称" />
      </el-form-item>
      <el-form-item label="社会运营中心电话 " prop="thirdDataTel">
        <el-input v-model="formData.thirdDataTel" placeholder="请输入社会运营中心电话 " />
      </el-form-item>
      <el-form-item label="第三方编号前缀（设备等编号前缀）" prop="codePrefix">
        <el-input v-model="formData.codePrefix" placeholder="请输入第三方编号前缀（设备等编号前缀）" />
      </el-form-item>
      <el-form-item label="是否在线（1在线，0离线）" prop="onLine">
        <el-input v-model="formData.onLine" placeholder="请输入是否在线（1在线，0离线）" />
      </el-form-item>
      <el-form-item label="过期时间" prop="expireTime">
        <el-date-picker
          v-model="formData.expireTime"
          type="date"
          value-format="x"
          placeholder="选择过期时间"
        />
      </el-form-item>
      <el-form-item label="受限制的设备类型编码（逗号隔开）" prop="limitDeviceCode">
        <el-input v-model="formData.limitDeviceCode" placeholder="请输入受限制的设备类型编码（逗号隔开）" />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-radio-group v-model="formData.status">
          <el-radio value="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="创建人" prop="createUser">
        <el-input v-model="formData.createUser" placeholder="请输入创建人" />
      </el-form-item>
      <el-form-item label="修改人" prop="updateUser">
        <el-input v-model="formData.updateUser" placeholder="请输入修改人" />
      </el-form-item>
      <el-form-item label="社会运营中心表id（cfs_third_control_center）" prop="thirdControlCenterId">
        <el-input v-model="formData.thirdControlCenterId" placeholder="请输入社会运营中心表id（cfs_third_control_center）" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { UserAppConfigApi, UserAppConfigVO } from '@/api/cfs/userappconfig'

/** 社会运营平台_app配置 表单 */
defineOptions({ name: 'UserAppConfigForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  appId: undefined,
  appKey: undefined,
  thirdId: undefined,
  areaCode: undefined,
  areaName: undefined,
  operatorId: undefined,
  thirdDataName: undefined,
  thirdDataTel: undefined,
  codePrefix: undefined,
  onLine: undefined,
  expireTime: undefined,
  limitDeviceCode: undefined,
  status: undefined,
  createUser: undefined,
  updateUser: undefined,
  thirdControlCenterId: undefined,
})
const formRules = reactive({
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await UserAppConfigApi.getUserAppConfig(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as UserAppConfigVO
    if (formType.value === 'create') {
      await UserAppConfigApi.createUserAppConfig(data)
      message.success(t('common.createSuccess'))
    } else {
      await UserAppConfigApi.updateUserAppConfig(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    appId: undefined,
    appKey: undefined,
    thirdId: undefined,
    areaCode: undefined,
    areaName: undefined,
    operatorId: undefined,
    thirdDataName: undefined,
    thirdDataTel: undefined,
    codePrefix: undefined,
    onLine: undefined,
    expireTime: undefined,
    limitDeviceCode: undefined,
    status: undefined,
    createUser: undefined,
    updateUser: undefined,
    thirdControlCenterId: undefined,
  }
  formRef.value?.resetFields()
}
</script>